import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DeleteResult, Repository, UpdateResult } from 'typeorm';
import { Bk_Subject } from './bk_subject.entity';

@Injectable()
export class SubjectService {
    constructor(
        @InjectRepository(Bk_Subject)
        private readonly subjectRepository: Repository<Bk_Subject>
    ) { }

    async add(subject: { subject_name: string }): Promise<Bk_Subject> {
        return await this.subjectRepository.save(subject);
    }

    async delete(id: number): Promise<DeleteResult> {
        return await this.subjectRepository.delete(id);
    }

    async update(id: number, subject: { subject_name: string }): Promise<UpdateResult> {
        return await this.subjectRepository.update(id, subject);
    }

    async findAll(query: any): Promise<[Bk_Subject[], number]> {
        return await this.subjectRepository.findAndCount({ relations: ["product"], order: { id: "ASC" }, skip: query.skip, take: query.take });
    }
}